using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Xml;

namespace Omniture.API
{
	public class PageItemCollection : List<PageItem>, IXmlable
	{
		#region Class Members

		private OmnitureProject _parent = null;		

		#endregion

		#region Constructors

		internal PageItemCollection(OmnitureProject parent)
		{			
			_parent = parent;			
		}

		#endregion

		#region Property Implementations
	

		[Browsable(false)]
		public OmnitureProject ParentItem
		{
			get { return _parent; }
		}

		#endregion

		#region Methods

		public PageItem NewItem()
		{
			PageItem retval = new PageItem(_parent);
			this.Add(retval);
			return retval;
		}

		public PageItem this[string name]
		{
			get
			{
				foreach(PageItem c in this)
				{
					if (string.Compare(c.PageName, name, true) == 0)
						return c;
				}
				return null;
			}
		}

		public PageItem GetById(int id)
		{
			foreach(PageItem c in this)
			{
				if(c.Id == id)
					return c;
			}
			return null;
		}

		public PageItem GetByUrl(string url)
		{
			foreach (PageItem c in this)
			{
				if (string.Compare(url.ToLower(), c.PageURL.ToLower(), true) == 0)
					return c;
			}
			return null;
		}

		public bool Contains(string name)
		{
			foreach(PageItem t in this)
			{
				if (string.Compare(t.PageName, name, true) == 0)        
					return true;
			}
			return false;
		}

		public bool Contains(int index)
		{
			return (0 <= index) && (index < this.Count);
		}

		public bool ContainsId(int columnId)
		{
			foreach (PageItem t in this)
			{
				if (t.Id == columnId)
					return true;
			}
			return false;
		}

		public int IndexOf(string name)
		{

			for (int ii = 0; ii < this.Count; ii++)
			{
				if (this[ii].PageName == name)
					return ii;
			}
			return -1;
		}

		public void Remove(string name)
		{
			PageItem removeItem = null;
			foreach (PageItem t in this)
			{
				if (string.Compare(t.PageName, name, true) == 0)
					removeItem = t;
			}

			if (removeItem != null)
				this.Remove(removeItem);
		}

		public int GetNextUniqueId()
		{
			int retval = 1;
			foreach (PageItem item in this)
			{
				if (retval <= item.Id)
					retval = item.Id + 1;
			}
			return retval;
		}

		public void Clean(ITrackItem item)
		{
			foreach(PageItem page in this)
			{
				page.Clean(item);
			}
		}

		#endregion
		
		#region IXmlable Members

		public virtual void LoadXml(XmlNode parentNode)
		{			
			foreach (XmlNode node in parentNode.ChildNodes)
			{
				PageItem newItem = new PageItem(_parent);
				this.Add(newItem);
				newItem.LoadXml(node);
			}
		}

		public virtual void SaveXml(XmlNode parentNode)
		{
			//Now loop and add children
			foreach (PageItem t in this)
			{
				XmlNode newNode = XmlHelper.AddNode(parentNode, "Page");
				t.SaveXml(newNode);
			}
		}

		#endregion

	}
}
